package com.example.demo.Controller;

import com.example.demo.domain.Student;
import com.example.demo.util.JdbcHelper;
import lombok.Value;
import org.springframework.web.bind.annotation.*;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@RestController
@RequestMapping("/Student")
public class StudentController {
    @GetMapping("/get")
    public String get(@RequestParam(value = "id") int id) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        //创建SQL语句
        String search = "SELECT * from StudentForSpring where id = " + id;
        //在连接上创建语句盒子对象
        Statement statement = conn.createStatement();
        try (statement; conn) {
            //执行SQL语句
            ResultSet results = statement.executeQuery(search);
            results.next();
            //将获取的对象参数写入预先创建的对象
            Student student = new Student(id,
                    results.getString("name"),
                    results.getString("no"),
                    results.getString("remarks")
            );
            return student.toString();
        }
    }
}
